package com.winning.devops.batch.writer.ch01;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;
import java.util.List;

/**
 * ItemWriter简介Demo配置
 *
 * @author chensj
 * @date 2020-03-07 上午1:17
 */
@Configuration
public class ItemWriterDemoConfig {

    @Autowired
    private JobBuilderFactory jobBuilderFactory;
    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Autowired
    @Qualifier("itemDemoWriter")
    private ItemWriter<? super String> itemDemoWriter;


    @Bean
    public Job itemWriterDemoJob() {
        return jobBuilderFactory.get("itemWriterDemoJob")
                .start(itemWriterDemoStep())
                .build();
    }

    @Bean
    public Step itemWriterDemoStep() {
        return stepBuilderFactory.get("itemWriterDemoStep")
                .<String, String>chunk(2)
                .reader(itemDemoReader())
                .writer(itemDemoWriter)
                .build();
    }

    @Bean
    public ItemDemoReader itemDemoReader() {
        // 传递数据
        List<String> list = Arrays.asList("java", "spring", "mybatis", "hibernate", "jpa", "apollo", "jenkins");
        return new ItemDemoReader(list);
    }
}
